package org.jeecg.modules.analysisachievement.dataEffectiveness.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.config.mybatis.TenantContext;
import org.jeecg.modules.analysisachievement.achclasses.vo.AchClassAchievementExamScoreVo;
import org.jeecg.modules.analysisachievement.dataEffectiveness.mapper.DataEffectivenessMapper;
import org.jeecg.modules.analysisachievement.dataEffectiveness.service.IDataEffectivenessMapperService;
import org.jeecg.modules.common.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;


/**
 * @Description: ach_achievement_exam
 * @author: baochangrong
 * @Date: 2022-05-28
 * @Version: V1.0
 */
@Service
public class DataEffectivenessMapperServiceImpl extends ServiceImpl<DataEffectivenessMapper, JSONObject> implements IDataEffectivenessMapperService {
    @Autowired
    DataEffectivenessMapper dataEffectivenessMapper;

    int yearCode = DateUtil.getYear();
    int yearCodeLast = DateUtil.getYear() - 4;
    String tenantId = TenantContext.getTenant();

    public JSONObject queryDataCheck(JSONObject jsonObject) {
        if (!jsonObject.containsKey("yearCode")) {
            yearCode = jsonObject.getInteger("yearCode");
        }
        if (!jsonObject.containsKey("yearCodeLast")) {
            yearCodeLast = jsonObject.getInteger("yearCodeLast");
        }
        JSONObject returJson = new JSONObject();
        JSONArray returArray = new JSONArray();
        JSONObject whereJson = new JSONObject();
        whereJson.put("yearCode", String.valueOf(yearCode));
        whereJson.put("yearCodeLast", String.valueOf(yearCodeLast));
        whereJson.put("tenantId", tenantId);
        /**
         * 按顺序依次检查
         * 1001、届级
         * 1002、学年
         * 1003、学期
         * 1004、科目
         * 1005、年级
         * 1006、班级
         * 1007、课表模板
         * 1008、教学楼和教室
         * 1009、老师
         * 1010、学生
         * 1011、分配老师
         * 1012、分配教室
         * 1013、部门检查
         * 1014、系统设置
         * 1015、冲突检查
         * 1016、空数据检查
         * 1017、试卷检查
         * 1018、考试检查
         */
        List<JSONObject> gradessionYear = dataEffectivenessMapper.queryGradesessionyear(whereJson);
        JSONObject gradessionYearReturn = extracted(gradessionYear, "1001");
        returArray.add(gradessionYearReturn);
        List<JSONObject> schoolYear = dataEffectivenessMapper.querySchoolYear(whereJson);
        JSONObject schoolYearReturn = extracted(schoolYear, "1002");
        returArray.add(schoolYearReturn);
        List<JSONObject> semester = dataEffectivenessMapper.querySemester(whereJson);
        JSONObject semesterReturn = extracted(semester, "1003");
        returArray.add(semesterReturn);
        List<JSONObject> couSubject = dataEffectivenessMapper.queryCouSubject(whereJson);
        JSONObject couSubjectReturn = extracted(couSubject, "1004");
        returArray.add(couSubjectReturn);
        List<JSONObject> couGrade = dataEffectivenessMapper.queryCouGrade(whereJson);
        JSONObject couGradeReturn = extracted(couGrade, "1005");
        returArray.add(couGradeReturn);
        List<JSONObject> couClass = dataEffectivenessMapper.queryCouClass(whereJson);
        JSONObject couClassReturn = extracted(couClass, "1006");
        returArray.add(couClassReturn);



        returJson.put("data", returArray);
        return returJson;
    }

    /**
     * 按顺序依次检查
     * 1001、届级
     * 1002、学年
     * 1003、学期
     * 1004、科目
     * 1005、年级
     * 1006、班级
     * 1007、课表模板
     * 1008、教学楼和教室
     * 1009、老师
     * 1010、学生
     * 1011、分配老师
     * 1012、分配教室
     * 1013、部门检查
     * 1014、系统设置
     * 1015、冲突检查
     * 1016、空数据检查
     * 1017、试卷检查
     * 1018、考试检查
     */
    private JSONObject extracted(List<JSONObject> dataEffectiveness, String typeCode) {
        String checkName = "";
        int countLink = 2;
        if (typeCode.equals("1001")) {
            checkName = "届级";
        } else if (typeCode.equals("1002")) {
            checkName = "学年";
        } else if (typeCode.equals("1003")) {
            checkName = "学期";
        } else if (typeCode.equals("1004")) {
            checkName = "科目";
            countLink = 1;
        } else if (typeCode.equals("1005")) {
            checkName = "年级";
            countLink = 1;
        } else if (typeCode.equals("1006")) {
            checkName = "班级";
            countLink = 1;
        }
        JSONObject dataEffectivenessJson = new JSONObject();
        if (dataEffectiveness != null && dataEffectiveness.size() > 0 && countLink == 2) {
            dataEffectivenessJson.put("code", "0000");
            int dataEffectivenessIndex = 0;
            JSONObject dataEffectiveness1 = dataEffectiveness.get(dataEffectivenessIndex);
            JSONObject dataEffectiveness2 = dataEffectiveness.get(dataEffectivenessIndex + 1);

            dataEffectivenessJson.put("checkCode", dataEffectiveness1.getString("checkCode"));
            dataEffectivenessJson.put("checkName", dataEffectiveness1.getString("checkName"));
            dataEffectivenessJson.put("checkDate", DateUtil.getNow("yyyy-MM-dd hh:mm:ss"));
            int flowRate = dataEffectiveness1.getInteger("flowRate1") + dataEffectiveness2.getInteger("flowRate2");
            dataEffectivenessJson.put("flowRate", flowRate);
            dataEffectivenessJson.put("passRate", "ok");
            if (flowRate < 60) {
                dataEffectivenessJson.put("passRate", "error");
            }
            dataEffectivenessJson.put("remarks", dataEffectiveness1.getString("remarks1") + "，" + dataEffectiveness2.getString("remarks2"));
        }else if(dataEffectiveness != null && dataEffectiveness.size() > 0 && countLink == 1){
            dataEffectivenessJson.put("code", "0000");
            int dataEffectivenessIndex = 0;
            JSONObject dataEffectiveness1 = dataEffectiveness.get(dataEffectivenessIndex);

            dataEffectivenessJson.put("checkCode", dataEffectiveness1.getString("checkCode"));
            dataEffectivenessJson.put("checkName", dataEffectiveness1.getString("checkName"));
            dataEffectivenessJson.put("checkDate", DateUtil.getNow("yyyy-MM-dd hh:mm:ss"));
            int flowRate = dataEffectiveness1.getInteger("flowRate1") + dataEffectiveness1.getInteger("flowRate2");
            dataEffectivenessJson.put("flowRate", flowRate);
            dataEffectivenessJson.put("passRate", "ok");
            if (flowRate < 60) {
                dataEffectivenessJson.put("passRate", "error");
            }
            dataEffectivenessJson.put("remarks", dataEffectiveness1.getString("remarks1") + "，" + dataEffectiveness1.getString("remarks2"));
        } else {
            dataEffectivenessJson.put("checkCode", typeCode);
            dataEffectivenessJson.put("checkName", checkName);
            dataEffectivenessJson.put("checkDate", DateUtil.getNow("yyyy-MM-dd hh24:mm:ss"));
            dataEffectivenessJson.put("flowRate", 0);
            dataEffectivenessJson.put("passRate", "error");
            dataEffectivenessJson.put("remarks", checkName+": 各项数据都没有设置，请设置初始数据后使用系统");
            dataEffectivenessJson.put("code", "9999");
        }
        return dataEffectivenessJson;
    }
}
